package com.wushi.lenovo.asia5b.app_ui.pay.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;

import com.mvp.base.BaseMvpActivity;
import com.mvp.base.BaseSimpleFragment;
import com.mvp.utils.DecimalUtil;
import com.mvp.utils.StringUtils;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_config.CommonConstant;
import com.wushi.lenovo.asia5b.app_di.APPComponentFactory;
import com.wushi.lenovo.asia5b.app_di.goods.mvpview.PayActivityView;
import com.wushi.lenovo.asia5b.app_di.goods.presenter.PayActivityPresenter;
import com.wushi.lenovo.asia5b.app_event.OrderPayEvent;
import com.wushi.lenovo.asia5b.app_model.credit.MemberBlanceModel;
import com.wushi.lenovo.asia5b.app_model.credit.bean.PayType;
import com.wushi.lenovo.asia5b.app_model.credit.bean.TopupInfoBean;
import com.wushi.lenovo.asia5b.app_ui.other.activity.MainActivity;
import com.wushi.lenovo.asia5b.app_ui.other.dialog.pay.PayResultDialog;
import com.wushi.lenovo.asia5b.app_ui.pay.fragment.PayCardFragment;
import com.wushi.lenovo.asia5b.app_ui.pay.fragment.PayFpxFragment;
import com.wushi.lenovo.asia5b.app_ui.pay.fragment.PayInsSelectFragment;
import com.wushi.lenovo.asia5b.app_ui.pay.fragment.PaySufficientFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import static java.lang.String.format;
import static timber.log.Timber.d;

/**
 * Created by zhangyuncai on 2020/3/4.
 * 支付页面
 */
public class PayActivity extends BaseMvpActivity<PayActivityView, PayActivityPresenter> implements PayActivityView {
    public String payCode = "";
    public TopupInfoBean topupInfoBean = null;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_pay;
    }

    @Override
    protected void constructComponent() {
        APPComponentFactory.getAppBaseComponent().inject(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        topupInfoBean = (TopupInfoBean) getIntent().getSerializableExtra(CommonConstant.TopupInfoBean);
        payCode = getIntent().getStringExtra("payCode");

        initView();

        initData();

        initEvent();
    }

    @Override
    public void getMemberBalanceResult(MemberBlanceModel model) {
        Fragment fragment = null;
        topupInfoBean.setMemberBlanceModel(model);
        if (TextUtils.equals(payCode, PayType.topup.name())) {//我的钱包
            //应付金额
            String subtractS = DecimalUtil.subtract(model.getBalance_value(), topupInfoBean.getTotalAmount());
            float subtract = StringUtils.parseFloat(subtractS);
            d(format("--->>>>>>>>---value:%s,total:%s,subtract:%s", model.getBalance_value() + "", topupInfoBean.getTotalAmount(), subtract + ""));
            if (subtract >= 0) {//余额充足
                fragment = BaseSimpleFragment.sNewStance(mActivity, PaySufficientFragment.class);
            } else {//余额不足
                fragment = BaseSimpleFragment.sNewStance(mActivity, PayInsSelectFragment.class);
            }
        } else if (TextUtils.equals(payCode, PayType.fpx.name())) {//FPX 转账
            fragment = BaseSimpleFragment.sNewStance(mActivity, PayFpxFragment.class);
        } else if (TextUtils.equals(payCode, PayType.creditcard.name())) {//信用卡
            fragment = BaseSimpleFragment.sNewStance(mActivity, PayCardFragment.class);
        }
        if (fragment != null) {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fl_pay_container, fragment)
                    .commit();
        }
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        getPresenter().getMemberBalance();
    }

    @Override
    protected void initEvent() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onOrderPayEvent(OrderPayEvent event) {
        if (event != null) {
            PayResultDialog dialog = new PayResultDialog(mActivity, event.resultModel, true);
            dialog.show();
            if (event.resultModel.isSuccess()||true) {
                dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        if(event.resultModel.isSuccess()) {
                            Intent intent = new Intent(mActivity, MainActivity.class);
                            mActivity.startActivity(intent);
                        }
                        finish();
                    }
                });
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
